//-----------------------------------------------------------------------
//<copyright file="KhachHangDAO.cs
//Code generate by tools.
//</copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Data.SqlClient;
using PMS.Entity;
using PMS.Exceptions;

namespace PMS.DAL
{
	public class KhachHangDAO : SQLDatabase, IKhachHangDAO
	{

		#region Create KhachHang methods

		public KhachHang CreateKhachHang(KhachHang khachHang)
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spKhachHangInsert");
				AddOutParameter(command, "@MaKH", SqlDbType.NVarChar, khachHang.MaKH);
				AddInParameter(command, "@TenKH", SqlDbType.NVarChar, khachHang.TenKH);
				AddInParameter(command, "@DiaChi", SqlDbType.NVarChar, khachHang.DiaChi);
				AddInParameter(command, "@TinhThanh", SqlDbType.NVarChar, khachHang.TinhThanh);
				AddInParameter(command, "@QuanHuyen", SqlDbType.NVarChar, khachHang.QuanHuyen);
				AddInParameter(command, "@SoDT", SqlDbType.NVarChar, khachHang.SoDT);
				AddInParameter(command, "@SoFax", SqlDbType.NVarChar, khachHang.SoFax);
				AddInParameter(command, "@MaSoThue", SqlDbType.NVarChar, khachHang.MaSoThue);
				AddInParameter(command, "@Email", SqlDbType.NVarChar, khachHang.Email);
				AddInParameter(command, "@Website", SqlDbType.NVarChar, khachHang.Website);
				AddInParameter(command, "@GhiChu", SqlDbType.NVarChar, khachHang.GhiChu);

				ExecuteNonQuery(command);
                khachHang.MaKH = (string)GetParameterValue(command, "@MaKH");
				return khachHang;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access CreateKhachHangException", ex);
			}
		}

		#endregion

		#region Update KhachHang methods

		public void UpdateKhachHang(KhachHang khachHang)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spKhachHangUpdate");

				AddInParameter(command, "@MaKH", SqlDbType.NVarChar, khachHang.MaKH);
				AddInParameter(command, "@TenKH", SqlDbType.NVarChar, khachHang.TenKH);
				AddInParameter(command, "@DiaChi", SqlDbType.NVarChar, khachHang.DiaChi);
				AddInParameter(command, "@TinhThanh", SqlDbType.NVarChar, khachHang.TinhThanh);
				AddInParameter(command, "@QuanHuyen", SqlDbType.NVarChar, khachHang.QuanHuyen);
				AddInParameter(command, "@SoDT", SqlDbType.NVarChar, khachHang.SoDT);
				AddInParameter(command, "@SoFax", SqlDbType.NVarChar, khachHang.SoFax);
				AddInParameter(command, "@MaSoThue", SqlDbType.NVarChar, khachHang.MaSoThue);
				AddInParameter(command, "@Email", SqlDbType.NVarChar, khachHang.Email);
				AddInParameter(command, "@Website", SqlDbType.NVarChar, khachHang.Website);
				AddInParameter(command, "@GhiChu", SqlDbType.NVarChar, khachHang.GhiChu);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access UpdateKhachHangException", ex);
			}
		}

		#endregion

		#region Delete KhachHang methods

		public void DeleteKhachHang(KhachHang khachHang)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spKhachHangDelete");

				AddInParameter(command, "@MaKH", SqlDbType.NVarChar, khachHang.MaKH);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access DeleteKhachHangException", ex);
			}
		}

		#endregion

		#region Select methods

		#region GetAll KhachHang methods

		public KhachHangs GetAllKhachHang()
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spKhachHangGetList");
				KhachHangs Result = new KhachHangs();
				List<KhachHang> List = ExecuteQuery<KhachHang>(command,true);
				foreach(KhachHang item in List)
				{
					Result.Add(item);
				}
				return Result;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetAllKhachHangException", ex);
			}
		}

		#endregion


		#region GetKhachHang methods

		public KhachHang GetKhachHangByID(string maKH)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spKhachHangGet");

				AddInParameter(command, "@maKH", SqlDbType.NVarChar, maKH);

				return ExecuteQuery<KhachHang>(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetKhachHangBy ID Exception", ex);
			}
		}

		#endregion


		#endregion
	}
}//End Class
